package georah.map.tools
{

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	/**
	 *
	 * @author yuanm
	 */
	public final class KeyboardDefaults extends Navigation
	{
		override public final function get keyboardLocked():Boolean
		{
			return true;
		}

		override public final function get mouseLocked():Boolean
		{
			return false;
		}

		override protected final function this_disableHandler(event:MapToolEvent):void
		{
			map.removeEventListener(Event.ENTER_FRAME, map_enterFrameHandler);
			map.removeEventListener(KeyboardEvent.KEY_UP, map_keyUpHandler);

			super.this_disableHandler(event);
		}

		override protected final function this_enableHandler(event:MapToolEvent):void
		{
			super.this_enableHandler(event);

			map_enterFrameHandler();
			map.addEventListener(Event.ENTER_FRAME, map_enterFrameHandler);
			map.addEventListener(KeyboardEvent.KEY_UP, map_keyUpHandler);
		}

		private final function map_enterFrameHandler(event:Event=null):void
		{
			map.setFocus();
		}

		private final function map_keyUpHandler(event:KeyboardEvent):void
		{
			var action:int=0;
			switch (event.keyCode)
			{
				case Keyboard.UP:
					panInDirection(Navigation.PAN_UP);
					break;
				case Keyboard.DOWN:
					panInDirection(Navigation.PAN_DOWN);
					break;
				case Keyboard.LEFT:
					panInDirection(Navigation.PAN_LEFT);
					break;
				case Keyboard.RIGHT:
					panInDirection(Navigation.PAN_RIGHT);
					break;
			}
			switch (String.fromCharCode(event.charCode))
			{
				case "W":
				case "w":
					panInDirection(Navigation.PAN_UP);
					break;
				case "S":
				case "s":
					panInDirection(Navigation.PAN_DOWN);
					break;
				case "A":
				case "a":
					panInDirection(Navigation.PAN_LEFT);
					break;
				case "D":
				case "d":
					panInDirection(Navigation.PAN_RIGHT);
					break;
				case "+":
				case "=":
					zoom(Navigation.ZOOM_IN);
					break;
				case "-":
				case "_":
					zoom(Navigation.ZOOM_OUT);
					break;
			}
		}

	}
}
